import { type Directive } from "vue"
import { useUserStoreHook } from "@/store/modules/user"

/** 权限指令，和权限判断函数 checkAuths 功能类似 */
export const checkAuths: Directive = {
  mounted(el, binding) {
    const { value: checkAuths } = binding
    const { auths } = useUserStoreHook()
    if (Array.isArray(checkAuths) && checkAuths.length > 0) {
      const hasPermission = auths.some((role) => checkAuths.includes(role))
      // hasPermission || (el.style.display = "none") // 隐藏
      hasPermission || el.parentNode?.removeChild(el) // 销毁
    } else {
      throw new Error(`need auths! Like v-checkAuths(['cut','copy'])`)
    }
  }
}
